package com.fulihui.common.builder.web;

import com.fulihui.common.result.web.PageView;

import java.util.List;

/**
 * 用户web端返回对象构建
 *
 * @author: Levon
 * @version: v 0.1 2018-06-08 11:09
 */
public class PageViewBuilder {

    public static <T> PageView<T> build(List<T> values, int page, int rows, int totalSize) {
        int totalPage = totalSize % rows == 0 ? totalSize / rows : totalSize / rows + 1;
        totalPage = totalPage > 0 ? totalPage : 1;
        return build(values, page, rows, totalSize, totalPage);
    }

    public static <T> PageView<T> build(List<T> values, int page, int rows, int totalSize,
                                        int totalPage) {
        PageView<T> pageView = new PageView<>();
        if (page > 0) {
            pageView.setPage(page);
        }
        if (rows > 0) {
            pageView.setRows(rows);
        }
        pageView.setTotalSize(totalSize);
        pageView.setTotalRows(totalPage > 0 ? totalPage : 1);
        pageView.setValues(values);
        return pageView;
    }
}